<script>
let preorder = [3,9,20,15,7]
let inorder = [9,3,15,20,7]
var r = buildTree(preorder, inorder)
console.log(r)

function Node(value){
    this.value = value
    this.left = null
    this.right = null
}

function buildTree(preorder, inorder) {
    return foo(preorder, inorder)

};
function foo(preorder, inorder){
    if(preorder.length <= 0) return null
    let root = new Node(preorder[0])
    let i = inorder.indexOf(preorder[0]) //在中序遍历中找出前序第一个元素的位置
    let left = foo(preorder.slice(1, 1+i), inorder.slice(0, i))
    let right = foo(preorder.slice(1+i), inorder.slice(i+1))
    root.left = left
    root.right = right
    return root
}

</script>